layui.use(['form', 'jquery', 'layer', 'table', 'laydate','upload'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var upload = layui.upload;
	var staff_id = '';
	if (supplier_form_value.staff_id != undefined) {
		staff_id = supplier_form_value.staff_id;
		supplier_form_value.master_supplier_goods = supplier_form_value.master_supplier_goods == 1 ? true : false;
		if (supplier_form_value.goods_alone == 1) {
			$('.goods-audit-div').hide();
			supplier_form_value.goods_alone = true;
			supplier_form_value.goods_audit = false;
		} else {
			$('.goods-audit-div').show();
			supplier_form_value.goods_alone = false;
			supplier_form_value.goods_audit = supplier_form_value.goods_audit == 1 ? true : false;
		}
	}
	upload.render({
		elem: '#supplier_pictures_upload' + staff_id,
		url: upload_uri,
		accept: 'images',
		acceptMime: 'image/*',
		auto: true,
		field: 'supplier_pictures_upload' + staff_id,
		multiple: true,
		drag: true,
		before: function(obj) {
			layer.load();
		},
		done: function(res, index, upload) {
			if(res.code == 0) {
				var item = this.item;
				if(item == undefined){
					item = $('.layui-upload-drag');
				}
				var html = createUploadHtml('supplier_picture', res.data);
				$('#layui-upload-list-supplier' + staff_id).html(html);
				layer.msg('上传成功', {
					icon: 1
				});
			} else {
				layer.msg(e.msg, {
					icon: 2
				});
			}
	
			layer.closeAll('loading');
		},
		error: function(index, upload) {
			layer.msg('网络错误', {
				icon: 2
			});
			layer.closeAll('loading');
		}
	});
	// 初始化图片
	if(supplier_form_value.zhang_picture_info != undefined && supplier_form_value.zhang_picture_info) {
		console.log('1111');
		var html = createUploadHtml('supplier_picture', supplier_form_value.zhang_picture_info);
		console.log(html);
		$('#layui-upload-list-supplier' + staff_id).html(html);
	}
	var lay_filter = 'tpl_supplier' + staff_id;
	form.val(lay_filter, supplier_form_value);
	if(supplier_form_value.contract_file != null && supplier_form_value.contract_file != ''){
		$('#layui-upload-list'+staff_id).html(`
			<span class="invoice-link">
				<a href="${supplier_form_value.contract_file}" style="color: #ffffff;" target="_blank"><i class="layui-icon layui-icon-link"></i>查看合同</a>
			</span>
		`)
	}
	// 表单渲染
	form.render('checkbox');
	form.render('select');
	
	
	laydate.render({
		elem:"#contract_start"
	})
	laydate.render({
		elem:"#contract_end"
	})
	
	// 上传送货单
	var upload_hetong = upload.render({
		elem: '#contract_file'+staff_id,
		url: upload_rename_uri,
		accept: 'file',
		exts: 'pdf|doc|docx',
		acceptMime:'application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			$('#contract_file').val(res.data.save_path)
			$('#layui-upload-list'+staff_id).html(`
				<span class="invoice-link">
					<a href="${res.data.save_path}" style="color: #ffffff;" target="_blank"><i class="layui-icon layui-icon-link"></i>查看合同</a>
				</span>
			`)
			layer.closeAll('loading');
		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});
	

	// 监听复选框
	form.on('checkbox(supplier_goods_alone)', function(data) {
		var check_name = $(this).attr('name');
		var value = data.elem.checked;
		if (value == true) {
			$('.goods-audit-div').hide();
		} else {
			$('.goods-audit-div').show();
		}
	})
	// 监听提交
	form.on('submit(submit_supplier)', function(data) {
		var field = data.field;
		var url = $('form[lay-filter="' + lay_filter + '"]').attr('action');
		send_info(url, field, function(data) {
			if (data.code == 0) {
				layer.msg('保存成功', {
					icon: 1
				});
				// 关闭当前页 刷新父页
				$('.layui-body .layui-tab[lay-filter="content"] li.layui-this .layui-tab-close').trigger('click');
				$('.layui-body .layui-tab[lay-filter="content"] li[lay-id="supplier"]').trigger('click');
				// 父页面点击
				$('.layui-body .layui-tab[lay-filter="content"] li[lay-id="supplier_list"]').trigger('click');
				// 表格重载
				if ($('table[lay-filter="supplier_list"]').length > 0) {
					if (field.staff_id == undefined || field.staff_id == '') {
						table.reload('supplier_list');
					} else {
						table.editTr('supplier_list', 'staff_id', field.staff_id);
					}
				}
			} else {
				layer.msg(data.msg, {
					icon: 2,
					anim: 6
				});
			}
		})
		return false;
	})

})
